package org.opu.gui.component;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

/**
 *
 * @author yuriy
 */
public class SplitPanelWithToogle extends JPanel{
    public static final double MAX_SIZE = 1.0;
    private MainSplitPanel splitPanel;
    private JPanel statusPane;
    private Map<String, ButtonAndTab> buttons;

    /**
     * Create new instanceses
     * @param main main pane
     */
    public SplitPanelWithToogle(JComponent main) {
        super(new BorderLayout());
        buttons = new Hashtable<String, ButtonAndTab>();
        statusPane = new JPanel();
        statusPane.setLayout(new BoxLayout(statusPane, BoxLayout.X_AXIS));
        splitPanel = new MainSplitPanel(main, new StatusImpl());
        splitPanel.addComponentListener(new SplitPaneComponentListener());
        add(splitPanel, BorderLayout.CENTER);
        add(statusPane, BorderLayout.SOUTH);
        maximizeMain();
    }

    /**
     *  
     * @param title
     * @param icon
     * @param component
     */
    public void addTab(String title, Icon icon, Component component) {
        this.addTab(title, icon, component, title);
    }

    /**
     *
     * @param title
     * @param icon
     * @param component
     * @param name
     */
    public void addTab(String title, Icon icon, Component component, String name) {
        JToggleButton buttn = getButton(title, icon, name);
        buttons.put(title, new ButtonAndTab(buttn, component));
        statusPane.add(buttn);
        statusPane.revalidate();
        statusPane.repaint();
    }

    /**
     * 
     * @param title
     */
    public void remove(String title){
        splitPanel.remove(title);
        ButtonAndTab btn = buttons.remove(title);
        if (splitPanel.isNowTabs()){
            maximizeMain();
        }
        if (btn != null) {
            statusPane.remove(btn.button);
        }
        splitPanel.repaint();
        statusPane.repaint();
    }

    /**
     *  Create new JToggleButton with ToggleButtonAction action
     * @param title name and tool tip text for button
     * @param icon icon for button
     * @param name text for button
     * @return new JToggleButton with ToggleButtonAction action
     */
    private JToggleButton getButton(String title, Icon icon, String name) {
        JToggleButton buttn = new JToggleButton(name, icon);
        buttn.addItemListener(new ItemListenerImpl());
        buttn.setName(title);
        buttn.setToolTipText(title);
        return buttn;
    }

    private void maximizeMain() {
        splitPanel.setDividerLocation(MAX_SIZE);
    }

    private void delimMain() {
        //splitPanel.resetToPreferredSizes();
        splitPanel.setDividerLocation(0.7);
        splitPanel.reinitAll();
    }

    private void rmTab(AbstractButton get) {
        Component remove = splitPanel.remove(get.getName());
        if (splitPanel.isNowTabs()) {
            maximizeMain();
        }
        splitPanel.reinitAll();
    }

    /**
     * Combines JToggleButton and some Component
     */
    class ButtonAndTab {
        final JToggleButton button;
        final Component tab;

        public ButtonAndTab(JToggleButton button, Component tab) {
            this.button = button;
            this.tab = tab;
        }
    }

    /**
     * Simple status:
     * do nothing when status seted and
     * when element removed: button, with name of removed
     * element, set deselected
     */
    class StatusImpl implements Status {
        
        @Override
        public void setStatus(String status) {
            
        }

        @Override
        public void elementRemoved(String status) {
            ButtonAndTab get = buttons.get(status);
            if (get != null){
                get.button.setSelected(false);
                rmTab(get.button);
            }
            
        }
    }

    /**
     * Action for toogle button
     */
     class ItemListenerImpl implements ItemListener {

         @Override
         public void itemStateChanged(ItemEvent e) {
             int state = e.getStateChange();
             AbstractButton button = (AbstractButton) e.getSource();

             if (state == ItemEvent.SELECTED) {
                ButtonAndTab get = buttons.get(button.getName());
                splitPanel.addTab(button.getName(), button.getIcon(), get.tab);
                delimMain();
             } else {
                rmTab(button);
             }
         }

    }

     /**
      * If component resized reinit it divider location
      */
    class SplitPaneComponentListener implements ComponentListener {

        @Override
        public void componentResized(ComponentEvent e) {
            if (splitPanel.isNowTabs()) {
                maximizeMain();
            } else {
                delimMain();
            }
        }

        @Override
        public void componentMoved(ComponentEvent e) {
        }

        @Override
        public void componentShown(ComponentEvent e) {
        }

        @Override
        public void componentHidden(ComponentEvent e) {
        }
    }

}
